<?php
/**
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1
 * @license    GNU/GPL
*/

// no direct access

defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.view');

/**
 * HTML View class for the HelloWorld Component
 *
 * @package    HelloWorld
 */

class RestoranViewRestoran extends JView
{
    function display($tpl = null)
    {

        $namaKota =& $this->get( 'Datas');
        $config_params =& JComponentHelper::getParams( 'com_restoran' );
        $url_server = $config_params->get('url_server');

        $nama_input = explode(" ", $namaKota);
                  //  echo "<br />nama inputan = ";print_r($nama_input);
                    $jumlah = count($nama_input);
                    if ($jumlah==1){
                        $nama_input = $nama_input[0];
                    }
                    else{
                        $nama_kota='';
                        for ($i=0;$i<$jumlah;$i++){
                            if ($i==0){
                                $nama_kota = $nama_input[$i];
                            }else{
                                $nama_kota = $nama_kota."%20".$nama_input[$i];
                            }
                        }
                        $nama_input = $nama_kota;
                    }

        $url = $url_server."food/".$nama_input."/1";
        

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $data = curl_exec($ch);     
        curl_close($ch);
        $xml2array = $this->fetch_xml($data);


        $this->assignRef( 'xml', $xml2array );

        parent::display($tpl);
        
    }
    /*
     * function ini digunakan untuk mengubah format xml menjadi array
     */
    function fetch_xml($xml){

    if(is_file($xml)){
        $xml_data = file_get_contents($xml);
    }
    else{
        $xml_data = $xml;
    }
    $parser = xml_parser_create();
    xml_parse_into_struct($parser, $xml_data, &$assoc_arr, &$idx_arr);
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    $root_tag = $assoc_arr[0]['tag'];
    $base_tag = strtolower($assoc_arr[1]['tag']);
    $i = 0;
    foreach($assoc_arr as $key => $element){
        if($element['tag'] != $root_tag){
            if(!preg_match('/^\s+$/', $element['value'])){
                $tag = strtolower($element['tag']);
                $items[$i][$tag] = $element['value'];
                if($tag == $base_tag){
                    $i++;
                }
            }
            elseif(isset($element['attributes'])){
                $items[$i]['id'] = $element['attributes']['ID'];
            }
        }
    }

    return $items;
} //end of function fetchxml

}
